package cn.initcap.concurrency.concurrency.immutable;

import cn.initcap.concurrency.annoations.ThreadSafe;
import com.google.common.collect.Maps;
import java.util.Collections;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

/**
 * 不使用final修饰变量，让变量不可变，编译不会报错，但是运行时会报错，这种方式是线程安全的。
 *
 * @author initcap
 * @date Created in 2018/6/15 PM6:36.
 */
@Slf4j
@ThreadSafe
public class ImmutableExcample2 {

    private static Map<Integer, Integer> map = Maps.newHashMap();

    static {
        map.put(1, 2);
        map.put(3, 4);
        map.put(5, 6);
        map = Collections.unmodifiableMap(map);
    }

    public static void main(String[] args) {
        map.put(1, 3);
        log.info("{}", map.get(1));
    }

    private void test(final int a) {
        //a = 1;
    }

}
